import pymysql
import tkinter
import tkmacosx
import time
from PIL import Image, ImageTk, ImageSequence
import gui_login
from tkinter.messagebox import *

def add_one_message(message):
    db = pymysql.connect(host='180.102.129.53', user='root', passwd='520hhy', db='hhy', charset='utf8')
    cursor = db.cursor()
    whattime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # 写入数据库 时间格式为时间戳
    sql = """insert into message(user,phone,title,content,time) values("{}","{}","{}","{}","{}");""".format(message[0], message[1], message[2], message[3],str(whattime))
    print(sql)
    cursor.execute(sql)
    db.commit()
    showinfo("", "提交成功，感谢你的反馈！")


class ShowMessage:
    def __init__(self, username):
        global root
        self.username = username
        self.frame = tkmacosx.SFrame(root, width=450, height=500)
        self.frame2 = tkmacosx.SFrame(root, width=350, height=500)
        #用户名
        username_str = tkinter.StringVar()
        username_str.set(self.username)
        self.uusername = tkinter.Entry(self.frame, bg="white", fg="black", textvariable=username_str)
        self.name = tkinter.Label(self.frame, text="用户名：", fg='black',font=("微软雅黑", 15, ''))
        # 联系方式
        self.uuphone = tkinter.Entry(self.frame,bg="white", fg="black")
        self.phone = tkinter.Label(self.frame, text="联系方式：", fg='black', font=("微软雅黑", 15, ''))
        # 反馈标题
        self.uutitle = tkinter.Entry(self.frame,bg="white", fg="black")
        self.title = tkinter.Label(self.frame, text="反馈标题：", fg='black', font=("微软雅黑", 15, ''))
        # 反馈内容
        self.uuare = tkinter.Label(self.frame,text="反馈内容：", fg='black', font=("微软雅黑", 15, ''))
        self.area = tkinter.Text(self.frame, height=15, width=55)
        # 提交按钮
        self.bt1 = tkmacosx.Button(self.frame, text="提交", width=200, height=50, command=self.get_message)
        # 图片展示
        self.img1 = Image.open(r'../img/library.jpg')
        self.img2 = gui_login.resize(500, 450, self.img1)
        self.img2 = ImageTk.PhotoImage(self.img2)
        self.img_label = tkinter.Label(self.frame2, image=self.img2)

    def get_message(self):
        messge = [self.uusername.get(),self.uuphone.get(),self.uutitle.get(),self.area.get("1.0","end")]
        add_one_message(messge)


    def grid(self):
        self.name.grid(row=0,column=0)
        self.uusername.grid(row=0,column=1)
        self.uuphone.grid(row=1,column=1)
        self.phone.grid(row=1,column=0)
        self.uutitle.grid(row=2,column=1)
        self.title.grid(row=2,column=0)
        self.uuare.grid(row=3,column=0)
        self.area.grid(row=4,column=0,columnspan=2)
        self.bt1.grid(row=5,column=1)
        self.img_label.grid(row=0,column=0)
        self.frame2.grid(row=1, column=0)
        self.frame.grid(row=1, column=1)


def create_liuyan(name):
    global root
    root = tkinter.Toplevel()
    root.title("算法可视化留言反馈平台")
    root.geometry("800x500")
    big_title = tkinter.Label(root, text="欢迎使用反馈平台，你的想法会让平台运行的更好！", fg='black', font=("微软雅黑", 20, 'bold'))
    big_title.grid(row=0,column=0,columnspan=2)
    c1 = ShowMessage(name)
    c1.grid()
    root.mainloop()


if __name__ == '__main__':
    create_liuyan("apple")
